package com.chuanyang.driverh.mainmy;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainmy.adapter.DriverDetailCommonLineAdapter;
import com.chuanyang.driverh.mainother.LookPhotoDetailActivity;
import com.chuanyang.driverh.responseresult.CommentData;
import com.chuanyang.driverh.responseresult.CommentResult;
import com.chuanyang.driverh.responseresult.DriverInfoData;
import com.chuanyang.driverh.responseresult.DriverInfoDetailData;
import com.chuanyang.driverh.responseresult.DriverInfoDetailInfoResult;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.WindowUtils;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


public class DriverInfoActivity extends BaseTitleActivity {
    private Dialog dialog;
    private ArrayList<CommentData.DataBean> comments;

    private ListView iv_driverinfo_commonlines;
    private String driverId;
    private HashMap params;
    private ImageView iv_comment_icon1;
    private TextView tv_comment_name1;
    private TextView tv_comment_time1;
    private TextView tv_comment_content1;
    private ImageView iv_comment_icon2;
    private TextView tv_comment_name2;
    private TextView tv_comment_time12;
    private TextView tv_comment_content2;
    private LinearLayout ll_comment1_div;
    private LinearLayout ll_comment2_div;
    private ImageView iv_driverinfo_icon;
    private TextView tv_driverinfo_name;
    private TextView tv_driverinfo_role;
    private TextView tv_driverinfo_carnum;
    private TextView tv_driverinfo_phone;
    private TextView tv_driverinfo_captity;
    private TextView tv_driverinfo_realcaptity;
    private TextView tv_driverinfo_cartype;
    private TextView tv_driverinfo_carstandard;
    private TextView tv_driverinfo_commentmore;
    private DriverDetailCommonLineAdapter driverDetailCommonLineAdapter;
    private int driverRole;//司机的角色 “1”队长  "2" 车队成员  "3"未加入车队
    private RatingBar rb_stars;
    private ImageView iv_driver_details_car_img3;
    private ImageView iv_driver_details_car_img2;
    private ImageView iv_driver_details_car_img1;
    private ImageView iv_driverinfo_contact;
    private DriverInfoDetailData data;
    private LinearLayout ll_driverinfo_com;
    private TextView tv_driverinfo_com;
    private View line_driverinfo_nocom;
    private boolean isCarMenmber;
    private TextView tv_driverinfo_daycap;
    private TextView tv_driverinfo_capweight;
    private TextView tv_driverinfo_carshipphoto;
    private TextView tv_driverinfo_carshiptype;
    private LinearLayout ll_driverinfo_shipinfo;
    private TextView tv_myship_hatchNum;
    private TextView tv_myship_draft;
    private TextView tv_myship_hatchsizevolume;



    @Override
    protected int getContentResId() {
        return R.layout.my_act_driverinfo;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        showActivityLoading();
        DriverInfoData driverinfo = BaseApplication.getInstance().getDriverInfo();
        Intent intent = getIntent();
        driverId = intent.getStringExtra("driverId");
        isCarMenmber = intent.getBooleanExtra("isCarTeamMenmber",true);
        //如果没有加入车队
        if (driverinfo.getFleet() == null) {
            driverRole = 3;
        }
        else {
            if (driverId.equals(driverinfo.getFleet().getFleetLeader()+"")) {//此处的driverid是点击车队成员图像时传递过来的
                //此时表示个人就是车队长)
                driverRole = 1;

            } else {
                driverRole = 2;


            }
        }
        if(isCarMenmber)
        {
            getTitleTextView().setText("司机信息");

        }
        else {
            getTitleTextView().setText("船长信息");

        }

        setBackImg();
        findView();

    }

    private void findView() {
        comments = new ArrayList();
        iv_driverinfo_icon = (ImageView) findViewById(R.id.iv_driverinfo_icon);
        tv_driverinfo_name = (TextView) findViewById(R.id.tv_driverinfo_name);
        //船只信息  车辆照片
        tv_driverinfo_carshipphoto = (TextView) findViewById(R.id.tv_driverinfo_carshipphoto);
        //运力还是吨位
        tv_driverinfo_capweight = (TextView) findViewById(R.id.tv_driverinfo_capweight);
        //日常载重
        tv_driverinfo_daycap = (TextView) findViewById(R.id.tv_driverinfo_daycap);
        //车型还是类型
        tv_driverinfo_carshiptype = (TextView) findViewById(R.id.tv_driverinfo_carshiptype);
        tv_driverinfo_role = (TextView) findViewById(R.id.tv_driverinfo_role);
        tv_driverinfo_carnum = (TextView) findViewById(R.id.tv_driverinfo_carnum);
        tv_driverinfo_phone = (TextView) findViewById(R.id.tv_driverinfo_phone);
        tv_driverinfo_captity = (TextView) findViewById(R.id.tv_driverinfo_captity);
        tv_driverinfo_realcaptity = (TextView) findViewById(R.id.tv_driverinfo_realcaptity);
        tv_driverinfo_cartype = (TextView) findViewById(R.id.tv_driverinfo_cartype);
        tv_driverinfo_carstandard = (TextView) findViewById(R.id.tv_driverinfo_carstandard);
        tv_driverinfo_commentmore = (TextView) findViewById(R.id.tv_driverinfo_commentmore);
        iv_driverinfo_commonlines = (ListView) findViewById(R.id.iv_driverinfo_commonlines);
        iv_driverinfo_contact = (ImageView) findViewById(R.id.iv_driverinfo_contact);
        //船的舱口信息
        ll_driverinfo_shipinfo = (LinearLayout) findViewById(R.id.ll_driverinfo_shipinfo);
        //船舱口数
        tv_myship_hatchNum = (TextView) findViewById(R.id.tv_myship_hatchNum);
        //满载吃水
        tv_myship_draft = (TextView) findViewById(R.id.tv_myship_draft);
        //舱口尺寸
        tv_myship_hatchsizevolume = (TextView) findViewById(R.id.tv_myship_hatchsizevolume);
        rb_stars = (RatingBar) findViewById(R.id.rb_stars);
        //存在挂靠公司就显示挂靠公司  否则就显示一条线
        ll_driverinfo_com = (LinearLayout) findViewById(R.id.ll_driverinfo_com);
        tv_driverinfo_com = (TextView) findViewById(R.id.tv_driverinfo_com);
        line_driverinfo_nocom = findViewById(R.id.line_driverinfo_nocom);
        //司机的车辆图片
        iv_driver_details_car_img1 = (ImageView) findViewById(R.id.iv_driver_details_car_img1);
        iv_driver_details_car_img2 = (ImageView) findViewById(R.id.iv_driver_details_car_img2);
        iv_driver_details_car_img3 = (ImageView) findViewById(R.id.iv_driver_details_car_img3);
        ratioImage(iv_driver_details_car_img1);
        ratioImage(iv_driver_details_car_img2);
        ratioImage(iv_driver_details_car_img3);
        iv_driver_details_car_img1.setOnClickListener(this);
        iv_driver_details_car_img2.setOnClickListener(this);
        iv_driver_details_car_img3.setOnClickListener(this);
        iv_comment_icon1 = (ImageView) findViewById(R.id.iv_comment_icon1);
        tv_comment_name1 = (TextView) findViewById(R.id.tv_comment_name1);
        tv_comment_time1 = (TextView) findViewById(R.id.tv_comment_time1);
        tv_comment_content1 = (TextView) findViewById(R.id.tv_comment_content1);
        iv_comment_icon2 = (ImageView) findViewById(R.id.iv_comment_icon2);
        tv_comment_name2 = (TextView) findViewById(R.id.tv_comment_name2);
        tv_comment_time12 = (TextView) findViewById(R.id.tv_comment_time2);
        tv_comment_content2 = (TextView) findViewById(R.id.tv_comment_content2);
        ll_comment1_div = (LinearLayout) findViewById(R.id.ll_comment1_div);
        ll_comment2_div = (LinearLayout) findViewById(R.id.ll_comment2_div);
        tv_driverinfo_commentmore.setOnClickListener(this);
        iv_driverinfo_contact.setOnClickListener(this);
        ArrayList<DriverInfoDetailData.DriverLinesBean> lines = new ArrayList();
        driverDetailCommonLineAdapter = new DriverDetailCommonLineAdapter(this, lines);
        iv_driverinfo_commonlines.setAdapter(driverDetailCommonLineAdapter);
        getComment();
        getDriverDetailInfo();

    }

    private void getComment() {
        DialogUtil.showLoadingDialog(activityContext,null);
        Map<String,String> params=new HashMap<String, String>();
        params.put("page", "1");
        params.put("length", "2");
        params.put("driverId", driverId);

        MyHttpUtil.getByDriverOrFleet(activityContext,params, new MyCallback<CommentResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(CommentResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    if (result.getData().getData() != null && result.getData().getData().size() > 0) {
                        comments.clear();
                        CommentData.DataBean dataBean1 = result.getData().getData().get(0);
                        BitmapUtil.showImgForNetPath( dataBean1.getUserImage(),iv_comment_icon1);
                        tv_comment_name1.setText(dataBean1.getUserName() == null ? "匿名" : dataBean1.getUserName());
                        tv_comment_time1.setText(dataBean1.getCreateTime() == null ? "" : dataBean1.getCreateTime());
                        tv_comment_content1.setText(dataBean1.getEDetail() == null ? "" : dataBean1.getEDetail());
                        ll_comment1_div.setVisibility(View.VISIBLE);

                        if (result.getData().getData().size() == 2) {
                            CommentData.DataBean dataBean2 = result.getData().getData().get(1);
                            BitmapUtil.showImgForNetPath( dataBean2.getUserImage(),iv_comment_icon2);
                            tv_comment_name1.setText(dataBean2.getUserName() == null ? "匿名" : dataBean2.getUserName());
                            tv_comment_time1.setText(dataBean2.getCreateTime() == null ? "" : dataBean2.getCreateTime());
                            tv_comment_content1.setText(dataBean2.getEDetail() == null ? "" : dataBean2.getEDetail());
                            ll_comment2_div.setVisibility(View.VISIBLE);
                        }

                    } else {
                        ll_comment1_div.setVisibility(View.GONE);
                        ll_comment2_div.setVisibility(View.GONE);
                    }
            }
            }
        });



    }

    private void getDriverDetailInfo() {
        DialogUtil.showLoadingDialog(activityContext,null);
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", driverId);

        MyHttpUtil.selectDriverInfo(activityContext,params, new MyCallback<DriverInfoDetailInfoResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(DriverInfoDetailInfoResult result, int id) {
                if (result.getCode() == ConstUtil.OK && result.getData() != null) {
                    data = result.getData();
                    fillView();
                    hideActivityLoading();
                }
            }
        });
    }

    private void fillView() {
        BitmapUtil.showImgForNetPath(data.getUserImage(),iv_driverinfo_icon);
        tv_driverinfo_name.setText(data.getUsername());
        if (driverRole == 1) {
            tv_driverinfo_role.setText("队长");
        } else if (driverRole == 2) {
            tv_driverinfo_role.setText("队员");


        } else {
            tv_driverinfo_role.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(data.getLogisticsCompanyName())) {
            ll_driverinfo_com.setVisibility(View.GONE);
            iv_driverinfo_commonlines.setVisibility(View.VISIBLE);
        }
        else {
            tv_driverinfo_com.setText(data.getLogisticsCompanyName());
        }
        if(isCarMenmber)
        {

            //填充车辆照片 和司机信息
            if (!TextUtils.isEmpty(data.getDriver().getCarPt1())) {
                BitmapUtil.showImgForNetPath(data.getDriver().getCarPt1() ,iv_driver_details_car_img1);
            } else {
                iv_driver_details_car_img1.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(data.getDriver().getCarPt2())) {
                BitmapUtil.showImgForNetPath( data.getDriver().getCarPt2() ,iv_driver_details_car_img2);

            } else {
                iv_driver_details_car_img2.setVisibility(View.GONE);

            }
            if (!TextUtils.isEmpty(data.getDriver().getCarPt3())) {
                BitmapUtil.showImgForNetPath( data.getDriver().getCarPt3() ,iv_driver_details_car_img3);

            } else {
                iv_driver_details_car_img3.setVisibility(View.GONE);

            }
            tv_driverinfo_realcaptity.setText(data.getDriver().getActualCapacity() + "t");
            ll_driverinfo_shipinfo.setVisibility(View.GONE);


        }
        else {
            if (!TextUtils.isEmpty(data.getDriver().getShipItemPt())) {
                BitmapUtil.showImgForNetPath( data.getDriver().getShipItemPt() ,iv_driver_details_car_img1);

            } else {
                iv_driver_details_car_img1.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(data.getDriver().getInsurancePt1())) {
                BitmapUtil.showImgForNetPath( data.getDriver().getInsurancePt1() ,iv_driver_details_car_img2);
            } else {
                iv_driver_details_car_img2.setVisibility(View.GONE);

            }
            iv_driver_details_car_img3.setVisibility(View.GONE);
            tv_driverinfo_carshipphoto.setText("船只信息");
            tv_driverinfo_capweight.setText("吨位");
            tv_driverinfo_daycap.setVisibility(View.GONE);
            tv_driverinfo_realcaptity.setVisibility(View.GONE);
            tv_driverinfo_carshiptype.setText("类型");
            tv_myship_hatchNum.setText(data.getDriver().getHatchNum()+"口");
            tv_myship_draft.setText(data.getDriver().getDraft()+"m");
            tv_myship_hatchsizevolume.setText(data.getDriver().getHatchSize()+"/"+data.getDriver().getHatchVolume()+"m³");
        }

        tv_driverinfo_carnum.setText(data.getDriver().getPlateNumber());
        tv_driverinfo_phone.setText(data.getPhone());
        if (data.getStar() != null) {
            rb_stars.setRating(data.getStar());
        }


        //填充车辆信息
        tv_driverinfo_captity.setText(data.getDriver().getCapacity() + "t");
        driverDetailCommonLineAdapter.setNewData(data.getDriverLines());
        tv_driverinfo_cartype.setText(data.getDriver().getCarType());
        tv_driverinfo_carstandard.setText(data.getDriver().getCarStandard());
    }

   /* private void dial(final String tel){
        dialog= DialogUtil.myDialog(activityContext, "拨打电话", "取消", "确定","确定拨打："+tel+"?", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.dialog_btn_left:
                        break;
                    case R.id.dialog_btn_right:
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + tel));
                        try {
                            if (PermissoinUtil.checkAndRequestPermission(activityContext, android.Manifest.permission.CALL_PHONE, "请给应用添加拨打电话的权限！"))
                                startActivity(intent);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
                dialog.cancel();
            }
        });
    }*/



    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_driverinfo_commentmore:
                Intent intent = new Intent(this, CommentActivity.class);
                intent.putExtra("driverId", driverId);
                intent.putExtra("fleetId", "");
                startActivity(intent);
                break;
            case R.id.iv_driverinfo_contact:
              DialogUtil.  dial(activityContext,data.getPhone());
                break;
            case R.id.iv_driver_details_car_img1:
                if(isCarMenmber) {
                    startActivity(LookPhotoDetailActivity.class, data.getDriver().getCarPt1());
                }
                else {
                    startActivity(LookPhotoDetailActivity.class, data.getDriver().getShipItemPt());

                }
                break;
            case R.id.iv_driver_details_car_img2:
                if(isCarMenmber) {
                    startActivity(LookPhotoDetailActivity.class, data.getDriver().getCarPt2());
                }
                else {
                    startActivity(LookPhotoDetailActivity.class, data.getDriver().getInsurancePt1());

                }
                break;
            case R.id.iv_driver_details_car_img3:
                startActivity(LookPhotoDetailActivity.class, data.getDriver().getCarPt3());
                break;
        }
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //如果存在车辆信息则显示该页面
        hideActivityLoading();
    }

    /**
     * 设置图片的高度和宽度相等，且每张图占屏幕面积的1/3
     *
     * @param img
     */
    private void ratioImage(ImageView img) {
        int srceenWide = WindowUtils.getScreenWide(this);
        float pix = WindowUtils.getPixel(this);
        img.measure(0, 0);
        int imgWide = (int) ((srceenWide - 40 * pix) / 4);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) img.getLayoutParams();
        params.width = imgWide;
        params.height = imgWide;
        img.requestLayout();
    }
}
